package cn.lili.controller.user.member;

import cn.hutool.core.collection.CollUtil;
import cn.lili.common.aop.annotation.DemoSite;
import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.member.entity.dto.*;
import cn.lili.modules.member.entity.vo.UserVO;
import cn.lili.modules.member.service.UserService;
import com.alipay.api.domain.UserDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 会员管理接口
 *
 * @author Bulbasaur
 * @since 2020-02-25 14:10:16
 */
@RestController
@Api(tags = "会员管理接口")
@RequestMapping("/user/member")
@RequiredArgsConstructor
@Validated
public class MemberUserController {
    private final UserService userService;

    @ApiOperation(value = "会员分页列表")
    @GetMapping
    public ResultMessage<Page<UserVO>> getByPage(UserSearchParams userSearchParams) {
        userSearchParams.setScene(SceneEnums.MEMBER);
        return ResultUtil.data(userService.getUserPage(userSearchParams, userSearchParams));
    }

    @ApiOperation(value = "查询会员导出列表")
    @GetMapping("/queryExportMember")
    public ResultMessage<List<MemberExportDTO>> queryExportMember(UserSearchParams userSearchParams) {
        userSearchParams.setScene(SceneEnums.MEMBER);
        return ResultUtil.data(userService.queryExportMember(userSearchParams));
    }

    @ApiOperation(value = "通过ID获取会员信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "/{id}")
    public ResultMessage<UserVO> get(@PathVariable String id) {
        User user = userService.getById(id);
        if ((user == null) || !SceneEnums.MEMBER.name().equals(user.getScene())) {
            return ResultUtil.data(new UserVO());
        }
        return ResultUtil.data(new UserVO(user));
    }

    /**
     * 修改是否免密支付
     * @param isPassword 是否免密支付
     * @return 是否修改成功
     */
    @PutMapping("/updateIsPassword")
    public ResultMessage<Object> updateIsPassword(@RequestParam Boolean isPassword)  {
        User user = new User();
        user.setId(UserContext.getCurrentId());
        user.setIsPassword(isPassword);
        return ResultUtil.data(userService.updateById(user));
    }

    /**
     * 查询免密支付
     * @return 查询免密支付
     */
    @GetMapping("/getIsPassword")
    public ResultMessage<Object> getIsPassword()  {
        User user = userService.getById(UserContext.getCurrentId());
        if (user.getIsPassword() == null) {
            user.setIsPassword(false);
        }
        return ResultUtil.data(user);
    }

    @ApiOperation(value = "添加会员")
    @PostMapping
    public ResultMessage<User> save(@Valid @RequestBody UserInfoDTO userInfoDTO) {
        userInfoDTO.setScene(SceneEnums.MEMBER);
        return ResultUtil.data(userService.addUser(userInfoDTO));
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "修改会员基本信息")
    @PutMapping
    @DemoSite
    public ResultMessage<UserVO> update(@Valid @RequestBody UserInfoDTO userInfoDTO) {
        userInfoDTO.setScene(SceneEnums.MEMBER);
        return ResultUtil.data(userService.updateUser(userInfoDTO));
    }

    /**
     * 清空上级ID
     * @param userId 用户ID
     * @return 是否成功
     */
    @PutMapping("/clearParent/{userId}")
    @PreventDuplicateSubmissions
    @ApiOperation(value = "清空会员上级")
    public ResultMessage<Object> clearParent(@PathVariable String userId) {
        return ResultUtil.data(userService.clearParent(userId));
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "会员状态修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userIds", value = "用户ID", required = true, dataType = "String", allowMultiple = true, paramType = "query"),
            @ApiImplicitParam(name = "enable", required = true, dataType = "boolean", paramType = "query")
    })
    @PutMapping("/status")
    @DemoSite
    public ResultMessage<Object> updateMemberStatus(@RequestBody UserStatusChangeDTO userStatusChangeDTO) {
        userStatusChangeDTO.setScene(SceneEnums.MEMBER);
        userService.updateUserStatus(userStatusChangeDTO);
        return ResultUtil.success();
    }


    @ApiOperation(value = "根据条件查询会员总数")
    @GetMapping("/num")
    public ResultMessage<Long> getNum(UserSearchParams userSearchParams) {
        userSearchParams.setScene(SceneEnums.MEMBER);
        return ResultUtil.data(userService.getMemberNum(userSearchParams));
    }

    /**
     * 用户下拉列表
     * @param name 用户名
     * @return 用户列表
     */
    @GetMapping("/select")
    public ResultMessage<Object> select(String name) {
        return ResultUtil.data(userService.select(name));
    }

    /**
     * 团队树
     */
    @GetMapping("/teamTree")
    public ResultMessage<Object> teamTree(String searchName) {
        return ResultUtil.data(userService.teamTree(searchName));
    }

    /**
     * 店主下级列表
     */
    @GetMapping("/getLowerUserList")
    @ApiOperation("店主下级列表")
    public ResultMessage<Object> getLowerUserList(@Validated UserQueryDTO dto) {
        return ResultUtil.data(userService.getLowerUserList(dto));
    }

    @GetMapping("/selectUserForCityDistributionByAccount")
    @ApiOperation("根据店主account搜索满足城主条件的用户")
    public ResultMessage<List<User>> selectUserForCityDistributionByAccount(UserQueryDTO dto) {
        if (StringUtils.isEmpty(dto.getAccount())) {
            throw new ServiceException("账号不可为空");
        }
        List<User> users = userService.selectUserForCityDistributionByAccount(dto);
        if (CollUtil.isNotEmpty(users)) {
            return ResultUtil.data(users);
        }
        return ResultUtil.data(new ArrayList<>());
    }


    @GetMapping("/isAuth")
    @ApiOperation("是否实名认证")
    public ResultMessage<Integer> isAuth () {
        return ResultUtil.data(userService.isAuth());
    }


}
